package com.tablepet.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.tablepet.R;
import com.tablepet.activities.App;
import com.tablepet.activities.preview.PipActivity;
import com.tablepet.bean.ShowcaseBean;
import com.tablepet.utils.Utils;

import java.util.List;
public class ShowcaseAdapter extends RecyclerView.Adapter<ShowcaseAdapter.VH> {
    @SuppressLint("StaticFieldLeak")
    private static Context context ;
    private final List<ShowcaseBean> mList;
    TextView tv;
    public ShowcaseAdapter(Context context, List<ShowcaseBean> mList) {
        this.mList = mList;
        ShowcaseAdapter.context = context;
    }
    @NonNull
    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_showcase, parent, false));
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    @SuppressLint({"SetTextI18n", "CommitPrefEdits"})
    @Override
    public void onBindViewHolder(@NonNull VH vh, int position) {
        vh.dir=mList.get(position).assetsDir;
        ((ImageView)vh.itemView.findViewById(R.id.item_img)).setImageResource(mList.get(position).id);
        tv=vh.itemView.findViewById(R.id.item_tv);
        String petDir = App.sp.getString("ChoosedPet", "null");
        if (petDir.equals(vh.dir)){
            tv.setText("Choosed");
        }else {
            tv.setText("Preview");
        }
        tv.setOnClickListener(v -> {
            if (!Utils.isActive(new Intent(context, PipActivity.class),context)){
                context.startActivity(new Intent(context, PipActivity.class));
            }
            if (tv.getText().toString().equals("Preview")){
                SharedPreferences.Editor edit = App.sp.edit();
                edit.putBoolean("isChoosed",false);
                edit.putString("PreviePet",vh.dir);
                edit.apply();
                App.configChange=true;
                tv.setText("Choose");
            }else if (tv.getText().toString().equals("Choose")){
                SharedPreferences.Editor edit1 = App.sp.edit();
                edit1.putBoolean("isChoosed", true);
                edit1.putString("ChoosedPet",vh.dir);
                edit1.apply();
                App.configChange = true;
                tv.setText("Choosed");
            }else {
                Utils.backToDesktop(context);
            }
        });
    }
    @Override
    public void onViewAttachedToWindow(@NonNull VH vh) {
        super.onViewAttachedToWindow(vh);
    }
    @Override
    public int getItemCount(){
        return mList.size();
    }
    static class VH extends RecyclerView.ViewHolder {
        String dir;
        View itemView;
        @SuppressLint("SetTextI18n")
        public VH(View itemView) {
            super(itemView);
            this.itemView=itemView;
        }
    }
}